<?php
// +—————————————————————————————————————————————————————————————————————
// | Created by Yunbao
// +—————————————————————————————————————————————————————————————————————
// | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
// +—————————————————————————————————————————————————————————————————————
// | Author: https://gitee.com/yunbaokeji
// +—————————————————————————————————————————————————————————————————————
// | Date: 2022-04-30
// +—————————————————————————————————————————————————————————————————————

namespace app\user\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

/**
 * Class AdminIndexController
 * @package app\user\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class AdminIndexController extends AdminBaseController{

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    
    public function index(){

        $content = hook_one('user_admin_index_view');

        if (!empty($content)) {
            return $content;
        }

        $list = Db::name('user')
            ->where(function (Query $query) {
				
				$query->where('user_type','2');
				 
                $data = $this->request->param();

                if($data['user_status']!=''){
                    $query->where('user_status', intval($data['user_status']));
                }

                if($data['isrecommend']!=''){
                    $query->where('isrecommend', intval($data['isrecommend']));
                }

                if($data['issuper']!=''){
                    $query->where('issuper', intval($data['issuper']));
                }

                if (!empty($data['uid'])) {
                    $query->where('id', intval($data['uid']));
                }

                if (!empty($data['keyword'])) {
                    $keyword = $data['keyword'];
                    $query->where('user_login|user_nicename|user_email|mobile', 'like', "%$keyword%");
                }

            })
            ->order("create_time DESC")
            ->paginate(20);


        $now=time();

        $list->each(function($v,$k){
            $v['user_login']=m_s($v['user_login']);
            $v['mobile']=m_s($v['mobile']);
            $v['user_email']=m_s($v['user_email']);

            if($v['vip_endtime']>$now){
                $v['vip_endtime_format']=date("Y-m-d",$v['vip_endtime']);
                $v['isvip']=1;
            }else{
                $v['vip_endtime_format']='--';
                $v['isvip']=0;
            }

            $country_list=$this->getCountrys();

            foreach ($country_list as $k1 => $v1) {
                if($v['country_code']==$v1['tel']){
                    $v['country_name']=$v1['name'];
                    break; 
                }
            }

            return $v;
        }); 
			
		
		//分页-->筛选条件参数
		$data = $this->request->param();
		$list->appends($data);
		
		
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }
	
	
	/*本站用户添加*/
	public function add(){
		$this->assign('country_list', $this->getCountrys());
		// 渲染模板输出
        return $this->fetch();	
		
	}
	
	
	public function add_post(){
		 if ($this->request->isPost()) {
            
            $data = $this->request->param();
			$user= Db::name('user');
			$country_code=$data['country_code'];
			$user_login=$data['user_login'];

            if(!$country_code){
                $this->error(lang("请选择国家/地区"));
            }

            if($user_login == ''){
                $this->error('请输入手机号');
            }else{
                if(!checkMobile($user_login)){
                    $this->error('请输入正确手机号');
                }
                
                $check = Db::name('user')
					->where(['user_login'=>$user_login,'country_code'=>$country_code])
					->find();
                if($check){
                    $this->error('该账号已存在');
                }
            }
			
			$user_nicename=$data['user_nicename'];
            if($user_nicename == ''){
                $this->error('请输入昵称');
            }else{
                $check = Db::name('user')
					->where("user_nicename='{$user_nicename}'")
					->find();
                if($check){
                    $this->error('昵称已存在');
                }
            }

			$data['user_type']=2;
			$data['user_pass']=cmf_password($data['user_pass']);
			$data['code']=createCode();
			$avatar=$data['avatar'];
			
			if($avatar==''){
				$data['avatar']= '/default.png'; 
				$data['avatar_thumb']= '/default_thumb.png';
                if($data['bg_img']==''){
                    $data['bg_img']= '/default.png'; 
                }
                
			}else if(strpos($avatar,'http')===0){
				/* 绝对路径 */
				$data['avatar']=  $avatar; 
				$data['avatar_thumb']=  $avatar;
                if($data['bg_img']==''){
                    $data['bg_img']= $avatar; 
                }
			}else if(strpos($avatar,'/')===0){
				/* 本地图片 */
				$data['avatar']=  $avatar;
				$data['avatar_thumb']=  $avatar; 
                if($data['bg_img']==''){
                    $data['bg_img']= $avatar; 
                }
			}

			$data['create_time']=time();

			$data['birthday']='2000-01-01';
			
			if(trim($data['signature'])==""){
				$data['signature']='这家伙很懒，什么都没留下';
			}
			
			$result=$user->insert($data);
			if($result!==false){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}					 


		}			
	}
	
	//编辑
	public function edit(){

        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('user')
            ->where("id={$id}")
            ->find();

        if(!$data){
            $this->error("信息错误");
        }
        
        $this->assign('data', $data);
        $this->assign('country_list', $this->getCountrys());
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $id=$data['id'];

            //获取用户的状态
            $user_status=Db::name("user")->where("id={$data['id']}")->value("user_status");

            $country_code=$data['country_code'];
            if(!$country_code){
                $this->error(lang("请选择国家/地区"));
            }


            $user_pass=$data['user_pass'];
            if($user_pass==''){
                unset($data['user_pass']);
            }else{
                $data['user_pass'] = cmf_password($user_pass);
            }
            
            
            
            $user_nicename=$data['user_nicename'];
            if($user_nicename == ''){
                $this->error('请输入昵称');
            }else if($user_status!=3 && strstr($user_nicename, '已注销')!==false){
                $this->error('非注销用户昵称不能包含已注销');
            }


            if($data['avatar']==''){
                $this->error('请上传头像/封面');
            }

            if($data['avatar_thumb']==''){
                $this->error('请上传头像缩略图');
            }

            if($data['bg_img']==''){
                $data['bg_img']=$data['avatar'];
            }
            
            $rs = DB::name('user')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }

            // $key='userinfo_'.$id;
            // delcache($key);
            $this->success("保存成功！");
        }
    }
	
	
	//删除用户
	public function del(){
        
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = DB::name('user')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        
        //删除关注列表
        Db::name("user_attention")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除关注消息
        Db::name("user_attention_messages")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除用户认证信息
        Db::name("user_auth")->where("uid='{$id}'")->delete();
        //删除用户拉黑
        Db::name("user_black")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除管理员充值用户记录
        Db::name("user_charge_admin")->where("touid='{$id}'")->delete();
        //删除音乐收藏
        Db::name("user_music_collection")->where("uid='{$id}'")->delete();
        //删除用户举报
        Db::name("user_report")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除用户token
        Db::name("user_token")->where("user_id='{$id}'")->delete();

        $list=Db::name("user_video")->field("id")->where("uid='{$id}'")->select()->toArray();

        foreach ($list as $k => $v) {
            //删除视频喜欢
            Db::name("user_video_like")->where("videoid='{$v['id']}'")->delete();

        }

        //删除用户视频
        Db::name("user_video")->where("uid='{$id}'")->delete();
        //删除视频评论
        Db::name("user_video_comments")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除视频评论@信息
        Db::name("user_video_comments_at_messages")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除视频评论点赞
        Db::name("user_video_comments_like")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除视频评论信息
        Db::name("user_video_comments_messages")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除视频喜欢
        Db::name("user_video_like")->where("uid='{$id}'")->delete();
        //删除视频付费列表
        Db::name("user_video_paylists")->where("uid='{$id}'")->delete();
        //删除视频举报
        Db::name("user_video_report")->where("uid='{$id}' or touid='{$id}'")->delete();
        //删除视频观看记录
        Db::name("user_video_view")->where("uid='{$id}'")->delete();
        //删除用户视频观看记录
        Db::name("user_video_watchlists")->where("uid='{$id}'")->delete();
        //删除用户视频观看时间记录
        Db::name("user_video_watchtime")->where("uid='{$id}'")->delete();
        //删除视频观看时长记录
        Db::name("view_reward")->where("uid='{$id}'")->delete();

        //删除反馈
        Db::name("feedback")->where("uid='{$id}'")->delete();
        //删除点赞信息表
        Db::name("praise_messages")->where("uid='{$id}' or touid='{$id}'")->delete();

        //删除系统消息
        Db::name("system_push")->where("uid='{$id}'")->delete();



        //删除用户的redis
        delcache("token_".$id);


        $this->success("删除成功！",url("user/adminIndex/index"));
    }

    /**
     * 本站用户禁用
     * @adminMenu(
     *     'name'   => '本站用户禁用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户禁用',
     *     'param'  => ''
     * )
     */
    public function ban(){

        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
            if ($result) {
                $this->success("会员禁用成功！");
            } else {
                $this->error('会员禁用失败,会员不存在,或者是管理员！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan(){

        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
            $this->success("会员启用成功！", '');
        } else {
            $this->error('数据传入失败！');
        }
    }




    public function getCountrys(){
        //读取国家代号
        $key='getCountrys';
        $info=getcaches($key);
        //$info=false;
        if(!$info){

            $country=CMF_ROOT.'data/config/country.json';
            // 从文件中读取数据到PHP变量 
            $json_string = file_get_contents($country); 
             // 用参数true把JSON字符串强制转成PHP数组 
            $data = json_decode($json_string, true);

            $info=$data['country']; //国家
            
            setcaches($key,$info);
        }

        $country_list=[];
        foreach ($info as $k => $v) {
            $arr=$v['lists'];
            foreach ($arr as $k1 => $v1) {
               $country_list[]=$v1;
            }
        }
        
        return $country_list;
    }

}
